<template>
	<view class="viewcontent">
		<view class="viewcontenthead">
			<view class="fl" @click="$UTILS.goBack(1)"></view>
			<view class="fr" style="color: #fdf5fa;"> 箱组信息绑定</view>
			<view class="leftitem"></view>
		</view>
		<view class="viewconten">
			<view class="viewcontenmes">
				<view class="viewcontenitem" v-for="(item,index) in this.itemlist" :key="index">
					<view class="mesitem"><text>序号：{{index+1}}</text><text style="margin-right: 15rpx;"
							@click="deleitem(item,index)">x</text></view>
					<view class="mesitem">
						<view class="leftmes">
							<view>物资名称：{{item.equipmentName}}</view>
						</view>
						<view class="rightmes">
							<view>物资编码：{{item.equipmentCode}}</view>
						</view>
					</view>
					<view class="mesitem">RFID:{{item.rfid}}</view>
				</view>
			</view>
			<view class="viewcontenfooter">
				<u-button @click="setRFID">确认绑定</u-button>
			</view>
		</view>
		<keyboard-listener @keydown="onKeydown"></keyboard-listener>
		<keyboard-listener @keyup="onKeyup"></keyboard-listener>
		<myLoading v-if="loading" style="position: absolute;z-index: 9;"></myLoading>
	</view>
</template>

<script>
	import keyboardListener from '../../../components/uniapp-keyboard-listener/src/keyboard-listener.vue'
	// #ifdef APP-PLUS
	// const pda = uni.requireNativePlugin("PDAModule");
	// const tone = uni.requireNativePlugin("ToneModule");
	// var log = uni.requireNativePlugin("LogModule")
	// #endif
	let andoridModule = uni.requireNativePlugin('seuic-uhf');
	export default {
		components: {
			keyboardListener
		},
		async onLoad(option) {

			this.itemlist = JSON.parse(option.demo)
			this.boxId = option.boxId
			//let [error, res] = await  this.$UTILS.requestRest(this.$COMMON.REST_URL.SUPPLIESRFIDS+`/${'E280117000000214F7E49B57'}`, 'GET')
		},
		onUnload() {

		},
		data() {
			return {
				itemlist: [],
				loading: false,
				lastQueryTimestamp: 0,
				rfidList: [],
				boxId: 0
			};
		},
		methods: {
			deleitem(item, index) {
				this.itemlist.splice(index, 1)
			},
			async pushitem() {

				let rfids = this.rfidList.join(',')
				let [error, res] = await this.$UTILS.requestRest(this.$COMMON.REST_URL.SUPPLIESRFIDS + `/${rfids}`,
					'GET')
					if (res.data?.code == 401) {
						let jqdizhi = this.$UTILS.getunlinelogin()
						let ictdizhi = this.$UTILS.getunlineaddress()
						uni.clearStorageSync()
						this.$UTILS.setunlinelogin(jqdizhi);
						this.$UTILS.setunlineaddress(ictdizhi);
						setTimeout(() => {
							uni.reLaunch({
								url: '/pages/view-layout/login/login',
							});
						}, 500)
						
						this.$UTILS.toast('token过期，请从新登录')
						return
					}
				if (res.data.result) {
					res.data.result.forEach(item => {
						let flg = this.itemlist.some(ite => {
							return item.rfid == ite.rfid
						})
						if (!flg) {
							this.itemlist.push(item)
						}
					})
				}
				this.loading = false

			},
			onKeydown(e) {
				if (e.keyCode == 123) {
					const now = Date.now();
					// 节流间隔，1000 毫秒
					const throttleDuration = 3000;
					if (now - this.lastQueryTimestamp < throttleDuration) {
						// 如果当前时间与最后一次执行的时间差小于节流间隔，则不执行
						return;
					}
					this.lastQueryTimestamp = now;
					// 	pda.getContinue(result => {
					// 	this.processData(result)
					// })
					andoridModule.inventoryOnce(1000, (e) => {
						if (e != '未寻到') {
							let jsonResult = JSON.parse(JSON.stringify(e));
							if (this.rfidList.indexOf(jsonResult.id) == -1) {
								this.rfidList.push(jsonResult.id)
							}
						} else {
							this.templedata = e
						}
					});
				}
			},
			onKeyup(e) {
				// pda.stop()
				// tone.setToneTimes(1, 100, 100)
				andoridModule.inventoryStop((e) => {
					this.templedata = e
				});
				this.loading = true
				this.pushitem()

			},
			processData(result) {
				let jsonResult = JSON.parse(result);
				if (this.rfidList.indexOf(jsonResult.epc) == -1) {
					this.rfidList.push(jsonResult.epc)
				}
			},
			async setRFID() {
				let boxBindEquipmentDTO = {}
				boxBindEquipmentDTO.boxId = this.boxId
				boxBindEquipmentDTO.equipmentList = this.itemlist.map(item => {
					return item.equipmentId
				})
				let [error, res] = await this.$UTILS.requestRest(this.$COMMON.REST_URL.SUPPLIESBIND, 'POST',boxBindEquipmentDTO)
				if (res && res.statusCode == 200) {
					if (res.data?.code == 401) {
						let jqdizhi = this.$UTILS.getunlinelogin()
						let ictdizhi = this.$UTILS.getunlineaddress()
						uni.clearStorageSync()
						this.$UTILS.setunlinelogin(jqdizhi);
						this.$UTILS.setunlineaddress(ictdizhi);
						setTimeout(() => {
							uni.reLaunch({
								url: '/pages/view-layout/login/login',
							});
						}, 500)
						
						this.$UTILS.toast('token过期，请从新登录')
						return
					}
					this.$UTILS.toast('上传成功')
					setTimeout(() => {
						uni.redirectTo({
							url: '/pages/view-working/Boxinformation/Boxinformation'
						})
					}, 500)

				} else {
					this.$UTILS.toast('数据上传失败')
				}
			},
		}
	}
</script>

<style lang="scss">
	.viewcontent {
		display: flex;
		flex-direction: column;
		width: 100%;
		height: 100vh;
		box-sizing: border-box;
		padding-top: 55rpx;

		.viewcontenthead {
			width: 100%;
			height: 5%;
			display: flex;
			justify-content: space-between;
			align-items: center;

			.fl {
				width: 100rpx;
				height: 10vh;
				line-height: 10vh;
				background: url("/static/right-arrow.png") no-repeat center;
				background-size: 14%;
				transform: rotate(180deg);
				cursor: pointer; // 增加鼠标指针样式
			}

			.fr {
				cursor: pointer; // 增加鼠标指针样式
			}

			.leftitem {
				width: 100rpx;
				display: flex;
				justify-content: center;
				z-index: 999;
			}
		}

		.viewconten {
			width: 100%;
			height: 95%;

			.viewcontenmes {
				width: 98%;
				height: 90%;
				margin: 2% auto;
				box-sizing: border-box;
				padding: 0 5rpx;
				font-family: PingFang SC;
				font-weight: 550;
				color: rgba(250, 249, 249, 0.8);
				font-size: 16px;
				overflow: auto;

				.viewcontenitem {
					width: 100%;
					height: 150rpx;
					border: 1px solid white;
					border-radius: 15px;
					margin-top: 16rpx;

					.mesitem {
						width: 100%;
						height: 33.3%;
						display: flex;
						flex-direction: row;
						align-items: center;
						box-sizing: border-box;
						justify-content: space-between;
						padding: 5rpx 15rpx;

						.leftmes {
							width: 50%;
							height: 100%;
							display: flex;
							flex-direction: row;
							align-items: center;

							view {
								text-overflow: ellipsis;
								overflow: hidden;
								white-space: nowrap;
							}
						}

						.rightmes {
							width: 50%;
							height: 100%;
							display: flex;
							flex-direction: row;
							align-items: center;

							view {
								text-overflow: ellipsis;
								overflow: hidden;
								white-space: nowrap;
							}
						}
					}
				}
			}

			.viewcontenfooter {
				width: 100%;
				height: 6%;
				display: flex;
				align-self: flex-end;

				button {
					width: 80%;
					height: 100%;
				}
			}
		}
	}
</style>